package com.example.service.appointment;

import cn.hutool.core.date.DateUtil;
import com.example.common.enums.RoleEnum;
import com.example.mapper.other.ChatGroupMapper;
import com.example.mapper.appointment.UserMapper;
import com.example.mapper.medical.DoctorMapper;
import com.example.mapper.other.ChatInfoMapper;
import com.example.mapper.other.ChatRoleMapper;
import com.example.model.entity.medical.Doctor;
import com.example.model.entity.other.Account;
import com.example.model.entity.other.ChatGroup;
import com.example.model.entity.appointment.User;
import com.example.model.entity.other.ChatInfo;
import com.example.model.entity.other.ChatRole;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 聊天组业务处理
 **/
@Service
public class ChatGroupService {

    @Resource
    private ChatGroupMapper chatGroupMapper;
    @Resource
    ChatInfoService chatInfoService;
    @Resource
    private ChatRoleMapper chatRoleMapper;
    @Resource
    private ChatInfoMapper chatInfoMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 新增
     */
    public void add(ChatGroup chatGroup) {
        ChatGroup dbChatGroup = chatGroupMapper.selectByChatUserIdAndUserId(chatGroup.getChatUserId(), chatGroup.getUserId());
        if (dbChatGroup == null) {
            chatGroupMapper.insert(chatGroup);
            ChatInfo chatInfo=new ChatInfo();
            chatInfo.setChatUserId(chatGroup.getUserId());
            chatInfo.setUserId(chatGroup.getChatUserId());
            chatInfo.setTime(DateUtil.now());
            ChatRole chatRole = chatRoleMapper.selectById(chatGroup.getChatUserId());
            String role = chatRole.getRole();
            if(role.equals(RoleEnum.DOCTOR.name())){
                chatInfo.setText("您最近有什么不舒服吗，请详细描述您的问题(2-100字)");
            }else {
               chatInfo.setText("有什么可以帮助您的吗？");
            }
            chatInfoMapper.insert(chatInfo);
        }
        ChatGroup dbChatGroup1 = chatGroupMapper.selectByChatUserIdAndUserId(chatGroup.getUserId(), chatGroup.getChatUserId());
        if (dbChatGroup1 == null) {
            dbChatGroup1 = new ChatGroup();
            dbChatGroup1.setChatUserId(chatGroup.getUserId());
            dbChatGroup1.setUserId(chatGroup.getChatUserId());
            chatGroupMapper.insert(dbChatGroup1);
        }
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        chatGroupMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            chatGroupMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(ChatGroup chatGroup) {
        chatGroupMapper.updateById(chatGroup);
    }

    /**
     * 根据ID查询
     */
    public ChatGroup selectById(Integer id) {
        return chatGroupMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<ChatGroup> selectAll(ChatGroup chatGroup) {
        return chatGroupMapper.selectAll(chatGroup);
    }

    /**
     * 分页查询
     */
    public PageInfo<ChatGroup> selectPage(ChatGroup chatGroup, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ChatGroup> list = chatGroupMapper.selectAll(chatGroup);
        return PageInfo.of(list);
    }

    public List<ChatGroup> selectUserGroup() {
        List<ChatGroup> chatGroupList = new ArrayList<>();
        Account currentUser = TokenUtils.getCurrentUser();
        ChatRole chatRole=null;
        if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            chatRole = chatRoleMapper.selectByDoctorId(currentUser.getId());

        }else if(RoleEnum.USER.name().equals(currentUser.getRole())){
            chatRole = chatRoleMapper.selectByUserId(currentUser.getId());
        }
        List<ChatGroup> userChatGroupList = chatGroupMapper.selectByUserId(chatRole.getId());
        for (ChatGroup chatGroup : userChatGroupList) {
            Integer chatUserId = chatGroup.getChatUserId();
            Integer num = chatInfoService.selectUnReadChatNum(currentUser.getId(), chatUserId);
            chatGroup.setChatNum(num);
            ChatRole role = chatRoleMapper.selectById(chatUserId);
            chatGroup.setChatUserName(role.getName());
            chatGroup.setChatUserAvatar(role.getAvatar());
            if(RoleEnum.BUSINESS.name().equals(role.getRole())){
                chatGroup.setChatUserId(role.getBusinessId());
                chatGroup.setRole(role.getRole());
            }else if(RoleEnum.DOCTOR.name().equals(role.getRole())){
                chatGroup.setChatUserId(role.getDoctorId());
                chatGroup.setRole(role.getRole());
            }else if(RoleEnum.USER.name().equals(role.getRole())){
                chatGroup.setChatUserId(role.getUsersId());
                chatGroup.setRole(role.getRole());
            }
            chatGroupList.add(chatGroup);
        }
        return chatGroupList;
    }


}
